package pyip.lib.mixtool.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketClient {

	public static void main(String[] args) {
		SocketClient client = new SocketClient();
		client.start();
	}

	private void start() {
		for (int i = 0; i < 5; i++) {

			Socket socket = null;
			PrintWriter writer = null;
			BufferedReader reader = null;
			try {
				socket = new Socket("127.0.0.1", 1000);

				writer = new PrintWriter(socket.getOutputStream());
				String sendline = "i am client, threadNum " + Thread.currentThread().getId() + "\n";
				System.out.println("client send: " + sendline);
				writer.write(sendline);
				writer.flush();

//				reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//				String line = reader.readLine();
//				System.out.println("client receive: " + line);
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (null != reader) {
					try {
						reader.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				if (null != writer) {
					// TODO no exception ??
					writer.close();
				}
				if (null != socket) {
					try {
						socket.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}

			// try {
			// Thread.sleep(1000);
			// } catch (InterruptedException e) {
			// // TODO Auto-generated catch block
			// e.printStackTrace();
			// }

		}
	}
}
